﻿#include "quicreator.h"
#include "ui_quicreator.h"
#include "qfile.h"
#include "qtranslator.h"
#include<QJsonParseError>
#include<QJsonDocument>
#include<QJsonObject>
#include<QDebug>
#include <QDesktopWidget>
#include<QImage>
#include<QPixmap>
#include<QPushButton>
#include<QMessageBox>
#include<QFileDialog>
#include<QPixmap>
#include<QPainter>
#include<QItemSelectionModel>
#include<QSqlRecord>
#include"xml/Inc/xmlstreamreader.h"
QUICreator::QUICreator(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::QUICreator)
{
    node = new dbNode();
    initDevice();
    initDataBase();
    ui->setupUi(this);
    QString host  = GetLocalIPAddress();
    node->ip = host;
    QDateTime current_date_time =QDateTime::currentDateTime();
    QString current_dateTime =current_date_time.toString("yyyy-MM-dd hh:mm:ss.zzz");
    node->loginTime = current_dateTime;
    this->initForm();
    //图片获取
    ui->widgetBottom->installEventFilter(this);
    void(Phoenix::*image)(Mat) = &Phoenix::getImage;
    void(Phoenix::*imageButtom)(Mat) = &Phoenix::getImageBottom;
    connect(m_Phoenix.get(),image,runUp,&QMyRunnable::showImage);
    //图片显示
    connect(m_Phoenix.get(),imageButtom,runBottom,&QMyRunnable::showImageBottom);
    void(QMyRunnable::*showUp)(QPixmap) = &QMyRunnable::printImgUp;
    void(QMyRunnable::*showBottom)(QPixmap) = &QMyRunnable::printImgBottom;
    connect(runUp,showUp,this,&QUICreator::showUp);
    connect(runBottom,showBottom,this,&QUICreator::showBottom);
    //折线图绘制
    void(BuffDetector::*real)(double) = &BuffDetector::getRealAngleSpeed;
    void(BuffDetector::*fir)(double) = &BuffDetector::getFIRAngleSpeed;
    connect(m_Phoenix->getWorkState()->getState()->getTaskOperation()->getBuffDetectorBottom().get(),
            real,&m_painMap,&paintMap::updateData0);
    connect(m_Phoenix->getWorkState()->getState()->getTaskOperation()->getBuffDetectorBottom().get(),
            fir,&m_painMap,&paintMap::updateData1);
    //参数改变
    connect(m_pVarManagerCamera,&QtVariantPropertyManager::valueChanged,this,&QUICreator::cameraValueChanged);
    connect(m_pVarManagerArmor,&QtVariantPropertyManager::valueChanged,this,&QUICreator::armorValueChanged);
    connect(m_pVarManagerBuff,&QtVariantPropertyManager::valueChanged,this,&QUICreator::buffValueChanged);
}
bool QUICreator::eventFilter(QObject *watched, QEvent *event)
{
    if(watched == ui->widgetBottom && event->type() == QEvent::Paint)
        paintBottom();
    return QWidget::eventFilter(watched, event);
}
QString QUICreator::GetLocalIPAddress()
{
    QString localHostName = QHostInfo::localHostName();
    QHostInfo info=QHostInfo::fromName(localHostName);
    for(int i=0;i<info.addresses().size();i++)
    {
        if(info.addresses().at(i).protocol() == QAbstractSocket::IPv4Protocol)
        {
            return info.addresses().at(i).toString();
        }
    }
}
void QUICreator::showUp(QPixmap pixMap)
{
    this->Up =pixMap;
    update();
}
void QUICreator::showBottom(QPixmap pixMap)
{
    this->Bottom =pixMap;
    update();
}
void QUICreator::paintBottom()
{
    QPainter painter(ui->widgetBottom);
    painter.drawPixmap(0,0,512,384,Bottom);
}
void QUICreator::paintEvent(QPaintEvent *)
{
    QPainter painter(this);
    int x1 = ui->widgetUp->pos().x();
    int y1 = ui->widgetUp->pos().y();
    painter.drawPixmap(x1,y1,ui->widgetUp->width(),ui->widgetUp->height(),Up);
}
void QUICreator::closeEvent(QCloseEvent *)
{
    QDateTime current_date_time =QDateTime::currentDateTime();
    QString current_dateTime =current_date_time.toString("yyyy-MM-dd hh:mm:ss.zzz");
    node->logoutTime = current_dateTime;
    QSqlDatabase db = m_dbModel->database();
    QSqlQuery query(db);
    QString insert = QString("insert into info values('%1','%2','%3','%4');").arg(node->ip).
                            arg(node->car).arg(node->loginTime).arg(node->logoutTime);
    qDebug() << insert << endl;
    bool value = query.exec(insert);
    if(!value)
    {
        QMessageBox::critical(NULL, "错误", "数据库添加失败： "+insert, QMessageBox::Yes, QMessageBox::Yes);
    }
}
void QUICreator::initXmlFile()
{
    m_pOpenFilePath = "/home/wzx/params.xml";
    QFile file("/home/wzx/params.xml");
    if(file.exists())
    {
        return ;
    }
    XmlStreamReader::creatXML("/home/wzx/params.xml");
    QStringList list;
    for(QMap<QtVariantProperty*,QString>::iterator iter = m_property_dic.begin();
        iter != m_property_dic.end();iter++)
    {
        QtVariantProperty* p = iter.key();
        QString s = iter.value();
        qDebug() << p->valueText() << endl;
        list << p->valueText();
    }
    XmlStreamReader::appendXML("/home/wzx/params.xml",list);
    int i = 0;
    m_pOpenFilePath = "/home/wzx/params.xml";
    QStringList listCamera,listArmor,listBuff;
    XmlStreamReader::readXML("/home/wzx/params.xml",listCamera,listArmor,listBuff);
    for(QMap<QtVariantProperty*,QString>::iterator iter = m_property_dic.begin();
        iter != m_property_dic.end();iter++)
    {
        QtVariantProperty* p = iter.key();
        QString s = iter.value();
        if(i < 6)
        {
            p->setValue(listCamera.at(i));
        }
        else if(i < 9)
        {
            p->setValue(listArmor.at(i - 6));
        }
        else if(i < 13)
        {
            p->setValue(listBuff.at(i - 9));
        }
        i++;
    }
}
QImage QUICreator::cvMat_to_QImage(const cv::Mat &mat )
{
  switch ( mat.type() )
  {
     // 8-bit, 4 channel
     case CV_8UC4:
     {
        QImage image( mat.data, mat.cols, mat.rows, mat.step, QImage::Format_RGB32 );
        return image;
     }

     // 8-bit, 3 channel
     case CV_8UC3:
     {
        QImage image( mat.data, mat.cols, mat.rows, mat.step, QImage::Format_RGB888 );
        return image.rgbSwapped();
     }

     // 8-bit, 1 channel
     case CV_8UC1:
     {
        static QVector<QRgb>  sColorTable;
        // only create our color table once
        if ( sColorTable.isEmpty() )
        {
           for ( int i = 0; i < 256; ++i )
              sColorTable.push_back( qRgb( i, i, i ) );
        }
        QImage image( mat.data, mat.cols, mat.rows, mat.step, QImage::Format_Indexed8 );
        image.setColorTable( sColorTable );
        return image;
     }

     default:
        qDebug("Image format is not supported: depth=%d and %d channels\n", mat.depth(), mat.channels());
        break;
  }
  return QImage();
}


void QUICreator::cameraValueChanged(QtProperty *property, const QVariant &value)
{
    if(property->propertyName() == "曝光时间")
    {
        m_mutex.lock();
        m_Phoenix->getMyCamera()->setVideoExposureTime(value.toInt());
        m_mutex.unlock();
    }
}
void QUICreator::armorValueChanged(QtProperty *property, const QVariant &value)
{
    if(property->propertyName() == "偏移量X：")
    {
        shared_ptr<Operation> operation = m_Phoenix->getWorkState()->getState()->getTaskOperation();
        if(operation!= nullptr)
        {
            m_mutex.lock();
            operation->getArmorOffSet()->setOffSetX(value.toInt());
            m_mutex.unlock();
        }
        else
        {
            //QMessageBox::about(NULL, "问题", "参数调整失败");
            qDebug() << "参数修改失败" << endl;
        }

    }
    else if(property->propertyName() == "偏移量Y：")
    {

        shared_ptr<Operation> operation = m_Phoenix->getWorkState()->getState()->getTaskOperation();
        if(operation!= nullptr)
        {
            m_mutex.lock();
            operation->getArmorOffSet()->setOffSetY(value.toInt());
            m_mutex.unlock();
        }
        else
        {
            //QMessageBox::critical(NULL, "错误", "参数修改失败", QMessageBox::Yes, QMessageBox::Yes);
            qDebug() << "参数修改失败" << endl;
        }
    }
}
void QUICreator::buffValueChanged(QtProperty *property, const QVariant &value)
{
    if(property->propertyName() == "偏移量X：")
    {
        shared_ptr<Operation> operation = m_Phoenix->getWorkState()->getState()->getTaskOperation();
        if(operation!= nullptr)
        {
            m_mutex.lock();
            operation->getBuffOffSet()->setOffSetX(value.toInt());
            m_mutex.unlock();
        }
        else
        {
            //QMessageBox::critical(NULL, "错误", "参数修改失败", QMessageBox::Yes, QMessageBox::Yes);
            qDebug() << "参数修改失败" << endl;
        }
    }
    else if(property->propertyName() == "偏移量Y：")
    {
        shared_ptr<Operation> operation = m_Phoenix->getWorkState()->getState()->getTaskOperation();
        if(operation!= nullptr)
        {
            m_mutex.lock();
            operation->getBuffOffSet()->setOffSetY(value.toInt());
            m_mutex.unlock();
        }
        else
        {
            //QMessageBox::critical(NULL, "错误", "参数修改失败", QMessageBox::Yes, QMessageBox::Yes);
            qDebug() << "参数修改失败" << endl;
        }
    }
    else if(property->propertyName() == "速度增益：")
    {

    }
}
QUICreator::~QUICreator()
{
    delete ui;
}
void QUICreator::initDevice()
{
    qRegisterMetaType<Mat>("Mat");
    QString vehicleStr = selectVehicle();
    string str = vehicleStr.toStdString();
    node->car = vehicleStr;
    shared_ptr<RMFactory> HDU_Phoenix_Factory((RMFactory*)Reflector::Instance()->CreateObject(str));
    m_Phoenix = HDU_Phoenix_Factory->creatPhoenix(); //实例化对象
    if(m_Phoenix != nullptr)
    {
        comThread.setParams(m_Phoenix);
        proThread.setParams(m_Phoenix);
        senthread.setParams(m_Phoenix);
        proBuffThread.setParams(m_Phoenix);
        proBuffThread.start();
        proThread.start();
        comThread.start();
        senthread.start();
    }
    while(m_Phoenix->getWorkState()->getState()->getTaskOperation()==nullptr);
    m_threadBottom = new QThread(this);
    m_threadUp = new QThread(this);

    runUp = new QMyRunnable(ui,m_Phoenix);
    runBottom = new QMyRunnable(ui,m_Phoenix);

    runUp->moveToThread(m_threadUp);
    runBottom->moveToThread(m_threadBottom);

    m_threadBottom->start();
    m_threadUp->start();

    m_thread = new QThread(this);
    m_painMap.moveToThread(m_thread);
    m_thread->start();
}
QString QUICreator::selectVehicle()
{
    QFile loadFile("/home/wzx/HDU_Phoenix/Phoenix.json");
    if(!loadFile.open(QIODevice::ReadOnly))
    {
        qDebug() << "could't open projects json";
        return 0;
    }
    QByteArray allData = loadFile.readAll();
    loadFile.close();
    QJsonParseError json_error;
    QJsonDocument jsonDoc(QJsonDocument::fromJson(allData, &json_error));
    if(json_error.error != QJsonParseError::NoError)
    {
        qDebug() << "json error!";
        return 0;
    }
    QJsonObject rootObj = jsonDoc.object();
    QStringList keys = rootObj.keys();
    QString str;
    if(rootObj.contains("name"))
    {
      str = rootObj["name"].toString();
    }
    return str;
}
void QUICreator::initForm()
{
    this->initTableViewDB();
    //this->initStyle();
    this->initCameraParams();
    this->initTranslator();
    this->initArmorParams();
    this->initBuffParams();
    this->initXmlFile();
    m_painMap.createCharts(ui);
    m_painMap.prepareData();
    ui->tabWidget->setCurrentIndex(0);
}
bool QUICreator::Mat2Qimage(cv::Mat&mat,QImage&qimage)
{
    if(mat.type()==CV_8UC3)
    {
       qimage=QImage((const unsigned char*)(mat.data),
                        mat.cols,mat.rows,
                        mat.step,
                        QImage::Format_RGB888).rgbSwapped();
    }
    else if(mat.type()==CV_8UC1)
    {
       static QVector<QRgb>  sColorTable;
       if(sColorTable.isEmpty())
       {
           for(int i=0;i<256;++i)
                sColorTable.push_back(qRgb(i,i,i));
       }
       qimage=QImage((const unsigned char*)(mat.data),
                     mat.cols,mat.rows,
                     mat.step,
                     QImage::Format_Indexed8);
       qimage.setColorTable(sColorTable);
    }
    else
    {
       return false;
    }
    return true;
}
void QUICreator::initCameraParams()
{

   m_pVarManagerCamera = new QtVariantPropertyManager(ui->widget_Camera);//关联factory，属性可以修改
   m_pVarFactory = new QtVariantEditorFactory(ui->widget_Camera);
   //connect(m_pVarManager,&QtVariantPropertyManager::valueChanged,this, &MainWindow::onValueChanged);//绑定信号槽，当值改变的时候会发送信号
   QtVariantProperty *groupItem  = m_pVarManagerCamera->addProperty(QtVariantPropertyManager::groupTypeId(),QStringLiteral("基本参数"));//添加组
    //属性
   QtVariantProperty *item = m_pVarManagerCamera->addProperty(QVariant::String,QStringLiteral("参数："));
   item->setValue("camera");
   item->setEnabled(false);
   groupItem->addSubProperty(item);
   m_property_dic[item] = QStringLiteral("参数：");

   item = m_pVarManagerCamera->addProperty(QVariant::Int,QStringLiteral("图片长X："));
   item->setValue(512);
   item->setEnabled(false);
   groupItem->addSubProperty(item);
   m_property_dic[item] = QStringLiteral("图片长X：");

   item = m_pVarManagerCamera->addProperty(QVariant::Int,QStringLiteral("图片长Y："));
   item->setValue(384);
   item->setEnabled(false);
   groupItem->addSubProperty(item);
   m_property_dic[item] = QStringLiteral("图片长Y：");

   item = m_pVarManagerCamera->addProperty(QVariant::Int,QStringLiteral("图片AOI X偏移"));
   item->setValue(64);
   item->setEnabled(false);
   groupItem->addSubProperty(item);
   m_property_dic[item] = QStringLiteral("图片AOI X偏移");


   item = m_pVarManagerCamera->addProperty(QVariant::Int,QStringLiteral("图片AOI Y偏移"));
   item->setValue(32);
   item->setEnabled(false);
   groupItem->addSubProperty(item);
   m_property_dic[item] = QStringLiteral("图片AOI Y偏移");

   item = m_pVarManagerCamera->addProperty(QVariant::Int,QStringLiteral("曝光时间"));
   item->setValue(2000);
   item->setEnabled(true);
   groupItem->addSubProperty(item);
   m_property_dic[item] = QStringLiteral("曝光时间");

//   其余属性
//   item = m_pVarManager->addProperty(QVariant::Bool,QStringLiteral("布尔型数据："));
//   item->setValue(true);
//   groupItem->addSubProperty(item);
//   m_property_dic[item] = QStringLiteral("布尔型数据:");
//   item = m_pVarManager2->addProperty(QVariant::Bool,QStringLiteral("布尔型数据2："));
//   item->setValue(true);
//   groupItem->addSubProperty(item);
//   item =m_pVarManager->addProperty(QVariant::Double, QStringLiteral("浮点数据："));
//   item->setValue(3.1415926);
//   groupItem->addSubProperty(item);
//   m_property_dic[item] = QStringLiteral("浮点数据：");
//   item =m_pVarManager->addProperty(QVariant::String, QStringLiteral("字符串数据："));
//   item->setValue(QStringLiteral("尘中远"));
//   groupItem->addSubProperty(item);
//   m_property_dic[item] = QStringLiteral("字符串数据：");
   ui->widget_Camera->addProperty(groupItem);
   ui->widget_Camera->setFactoryForManager(m_pVarManagerCamera,m_pVarFactory);

}

void QUICreator::initArmorParams()
{
    m_pVarManagerArmor = new QtVariantPropertyManager(ui->widget_Armor);//关联factory，属性可以修改
    //m_pVarManager2 = new QtVariantPropertyManager(ui->widget_Armor);//这个管理器不关联factory，属性不可修改
    m_pVarFactory = new QtVariantEditorFactory(ui->widget_Armor);
    //connect(m_pVarManager,&QtVariantPropertyManager::valueChanged,this, &MainWindow::onValueChanged);//绑定信号槽，当值改变的时候会发送信号
    QtVariantProperty *groupItem  = m_pVarManagerArmor->addProperty(QtVariantPropertyManager::groupTypeId(),QStringLiteral("基本参数"));//添加组
     //属性
    QtVariantProperty *item =m_pVarManagerArmor->addProperty(QVariant::String, QStringLiteral("参数："));
    item->setValue(QStringLiteral("armor"));
    item->setEnabled(false);
    groupItem->addSubProperty(item);
    m_property_dic[item] = QStringLiteral("参数：");

    item =m_pVarManagerArmor->addProperty(QVariant::String, QStringLiteral("敌方颜色："));
    item->setValue("BLUE/RED");
    item->setEnabled(false);
    groupItem->addSubProperty(item);
    m_property_dic[item] = QStringLiteral("敌方颜色：");

    item = m_pVarManagerArmor->addProperty(QVariant::Int,QStringLiteral("偏移量X："));
    item->setValue(0);
    item->setEnabled(true);
    groupItem->addSubProperty(item);
    m_property_dic[item] = QStringLiteral("偏移量X：");

    item = m_pVarManagerArmor->addProperty(QVariant::Int,QStringLiteral("偏移量Y："));
    item->setValue(0);
    item->setEnabled(true);
    groupItem->addSubProperty(item);
    m_property_dic[item] = QStringLiteral("偏移量Y：");


    ui->widget_Armor->addProperty(groupItem);
    ui->widget_Armor->setFactoryForManager(m_pVarManagerArmor,m_pVarFactory);

}

void QUICreator::initBuffParams()
{
    m_pVarManagerBuff = new QtVariantPropertyManager(ui->widget_Buff);//关联factory，属性可以修改
    //m_pVarManager2 = new QtVariantPropertyManager(ui->widget_Buff);//这个管理器不关联factory，属性不可修改
    m_pVarFactory = new QtVariantEditorFactory(ui->widget_Buff);
    //connect(m_pVarManager,&QtVariantPropertyManager::valueChanged,this, &MainWindow::onValueChanged);//绑定信号槽，当值改变的时候会发送信号
    QtVariantProperty *groupItem  = m_pVarManagerBuff->addProperty(QtVariantPropertyManager::groupTypeId(),QStringLiteral("基本参数"));//添加组
     //属性
    QtVariantProperty *item = m_pVarManagerBuff->addProperty(QVariant::String,QStringLiteral("参数: "));
    item->setValue("buff");
    item->setEnabled(false);
    groupItem->addSubProperty(item);
    m_property_dic[item] = QStringLiteral("参数: ");

    item = m_pVarManagerBuff->addProperty(QVariant::Int,QStringLiteral("偏移量X："));
    item->setValue(0);
    item->setEnabled(true);
    groupItem->addSubProperty(item);
    m_property_dic[item] = QStringLiteral("偏移量X：");

    item = m_pVarManagerBuff->addProperty(QVariant::Int,QStringLiteral("偏移量Y："));
    item->setValue(0);
    item->setEnabled(true);
    groupItem->addSubProperty(item);
    m_property_dic[item] = QStringLiteral("偏移量Y：");

    item = m_pVarManagerBuff->addProperty(QVariant::Int,QStringLiteral("速度增益："));
    item->setValue(10);
    item->setEnabled(true);
    groupItem->addSubProperty(item);
    m_property_dic[item] = QStringLiteral("速度增益：");

    ui->widget_Buff->addProperty(groupItem);
    ui->widget_Buff->setFactoryForManager(m_pVarManagerBuff,m_pVarFactory);
}

void QUICreator::initTableViewDB()
{
    m_dbModel = new QSqlTableModel(this);
    m_dbModel->setTable("info");
    ui->tableViewDB->setEditTriggers(QAbstractItemView::NoEditTriggers);
    ui->tableViewDB->setModel(m_dbModel);
    for(int i =0; i<4 ;i++)
    {
        ui->tableViewDB->setColumnWidth(i,ui->tableViewDB->width()/4);
    }
    m_dbModel->setHeaderData(0,Qt::Horizontal,"ip地址");
    m_dbModel->setHeaderData(1,Qt::Horizontal,"车辆名称");
    m_dbModel->setHeaderData(2,Qt::Horizontal,"登陆时间");
    m_dbModel->setHeaderData(3,Qt::Horizontal,"退出时间");
    m_dbModel->select();
}

void QUICreator::initDataBase()
{
    QSqlDatabase db  = QSqlDatabase::addDatabase("QMYSQL");
    db.setHostName("127.0.0.1");
    db.setUserName("root");
    db.setPassword("123456");
    db.setDatabaseName("RM");
    if(!db.open())
    {
        QMessageBox::warning(this,"错误",db.lastError().text());
        return ;
    }
    else
    {
        qDebug() << "db setok !" << endl;
    }
}
void QUICreator::initStyle()
{
    //加载样式表
    QFile file(":/qss/psblack.css");
    if (file.open(QFile::ReadOnly)) {
        QString qss = QLatin1String(file.readAll());
        QString paletteColor = qss.mid(20, 7);
        qApp->setPalette(QPalette(QColor(paletteColor)));
        qApp->setStyleSheet(qss);
        file.close();
    }
}

void QUICreator::initTranslator()
{
    //加载鼠标右键菜单翻译文件
    QTranslator *translator1 = new QTranslator(qApp);
    translator1->load(":/image/qt_zh_CN.qm");
    qApp->installTranslator(translator1);

    //加载富文本框鼠标右键菜单翻译文件
    QTranslator *translator2 = new QTranslator(qApp);
    translator2->load(":/image/widgets.qm");
    qApp->installTranslator(translator2);
}

void QUICreator::on_OpenFile_clicked()
{
    QString filePath;
    //QString runPath = QCoreApplication::applicationDirPath();
    filePath = QFileDialog::getOpenFileName(this, QString("请选择文件夹"),
                           QString("/home/wzx/HDU_Phoenix"), //初始化路径
                           tr("XML 文件 (*.xml);;所有文件(*)")
                           );
    if(filePath == "")
    {
        return ;
    }
    else
    {
        int i= 0;
        m_pOpenFilePath = filePath;
        QStringList listCamera,listArmor,listBuff;
        XmlStreamReader::readXML(filePath,listCamera,listArmor,listBuff);
        for(QMap<QtVariantProperty*,QString>::iterator iter = m_property_dic.begin();
            iter != m_property_dic.end();iter++)
        {
            QtVariantProperty* p = iter.key();
            QString s = iter.value();
            if(i < 6)
            {
                p->setValue(listCamera.at(i));
            }
            else if(i < 9)
            {
                p->setValue(listArmor.at(i - 6));
            }
            else if(i < 13)
            {
                p->setValue(listBuff.at(i - 9));
            }
            i++;
        }
    }

}

void QUICreator::on_SaveFile_clicked()
{
    QFile::remove(m_pOpenFilePath);
    XmlStreamReader::creatXML(m_pOpenFilePath);
    QStringList list;
    for(QMap<QtVariantProperty*,QString>::iterator iter = m_property_dic.begin();
        iter != m_property_dic.end();iter++)
    {
        QtVariantProperty* p = iter.key();
        QString s = iter.value();
        qDebug() << p->valueText() << endl;
        list << p->valueText();
    }
    XmlStreamReader::appendXML(m_pOpenFilePath,list);
    QString strMessage = m_pOpenFilePath +"参数保存成功";
    QMessageBox::information(NULL, "保存参数", strMessage,
                             QMessageBox::Yes | QMessageBox::No, QMessageBox::Yes);
}

void QUICreator::on_SaveAsFile_clicked()
{
    QString filePath = QFileDialog::getSaveFileName(this, QString("请输入保存文件夹名称"),
                           QString("/home/wzx"), //初始化路径
                           tr("XML 文件 (*.xml);;所有文件(*)")
                           );
    QString fileType = filePath.right (3);
    if(fileType != "xml")
    {
        QMessageBox::critical(NULL, "错误", "请创建xml文件", QMessageBox::Yes, QMessageBox::Yes);
        qDebug() << "file type error!" << endl;
        return ;
    }
    XmlStreamReader::creatXML(filePath);
    QStringList list;
    for(QMap<QtVariantProperty*,QString>::iterator iter = m_property_dic.begin();
        iter != m_property_dic.end();iter++)
    {
        QtVariantProperty* p = iter.key();
        QString s = iter.value();
        qDebug() << p->valueText() << endl;
        list << p->valueText();
    }
    XmlStreamReader::appendXML(filePath,list);
}

void QUICreator::on_NewFile_clicked()
{
    QString filePath = QFileDialog::getSaveFileName(this, QString("请输入保存文件夹名称"),
                           QString("/home/wzx"), //初始化路径
                           tr("XML 文件 (*.xml);;所有文件(*)")
                           );
    QString fileType = filePath.right (3);
    if(fileType != "xml")
    {
        QMessageBox::critical(NULL, "错误", "请创建xml文件", QMessageBox::Yes, QMessageBox::Yes);
        qDebug() << "file type error!" << endl;
        return ;
    }
    XmlStreamReader::creatXML(filePath);
    QStringList list;
    for(QMap<QtVariantProperty*,QString>::iterator iter = m_property_dic.begin();
        iter != m_property_dic.end();iter++)
    {
        QtVariantProperty* p = iter.key();
        QString s = iter.value();
        qDebug() << p->valueText() << endl;
        list << p->valueText();
    }
    XmlStreamReader::appendXML(filePath,list);
}

void QUICreator::on_btnRED_clicked()
{
    m_Phoenix->getWorkState()->getState()->getTaskOperation()->
            getArmorDetector()->setEnemyColor(RED);
}

void QUICreator::on_btnBLUE_clicked()
{
    m_Phoenix->getWorkState()->getState()->getTaskOperation()->
            getArmorDetector()->setEnemyColor(BLUE);
}

void QUICreator::on_ButtonDBDelete_clicked()
{
    bool issure = QMessageBox::information(NULL, "information", "是否删除这条记录", QMessageBox::Yes | QMessageBox::No, QMessageBox::Yes);
    if(issure)
    {
        QItemSelectionModel *sModel = ui->tableViewDB->selectionModel();
        QModelIndexList modelList  = sModel->selectedRows();
        for(int i = 0 ;i < modelList.size(); i++)
        {
            m_dbModel->removeRow(modelList.at(i).row());
        }
    }
    else
        return;
}
